package com.xiaoyun.lzg.strategy.manager;

import com.github.junrar.Junrar;
import com.github.junrar.exception.RarException;
import com.xiaoyun.lzg.strategy.SuffixStrategy;
import com.xiaoyun.lzg.strategy.SuffixType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class RARModel extends SuffixStrategy {


    @Override
    public Object marker(String name, InputStream is) {
        String[] str = name.split("\\.");
        File outFile = new File(getFreeMarkerUtil().getTempName(str[0]));
        if (!outFile.exists()) {
            outFile.mkdirs();
        }
        try {
            Junrar.extract(is, outFile);
        } catch (RarException | IOException e) {
            //TODO
            log.info("不支持 rar-5 文件");
        }
        List<Map<String, Object>> result = mapSave(outFile);

//        String msg = UserContext.get();
//        UserContext.remove();
//        root.put("errorMsg", msg);

        return result;
    }

    @Override
    public String getType() {
        return SuffixType.RAR;
    }
}
